package com.winz.cultureapp.fragment;

import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.xrcv.ShowActivityAdapter;
import com.winz.cultureapp.base.BaseFragment;
import com.winz.cultureapp.entity.ShowActivityListBean;
import com.winz.cultureapp.utils.MarginDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 志愿活动--展示活动
 */
public class ShowActivityFragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener {
    @BindView(R.id.xrcv_show)
    public XRecyclerView mXrcv_show;

    private boolean isRefresh = true;
    private int pageSize = 10;
    private int currentPage = 1;
    private int totalPage = 0;
    private List<ShowActivityListBean.DataBean.ListBean> mDatas;
    private ShowActivityAdapter mAdapter;
    private String cid;


    public static ShowActivityFragment getInstance(){
        ShowActivityFragment fragment = new ShowActivityFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString("cid",cid);
//        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int contentView() {
        return R.layout.fragment_show_activity;
    }

    @Override
    protected void initViews(View view) {
        mDatas = new ArrayList<>();
        // XrecyclerView
        GridLayoutManager manager = new GridLayoutManager(mContext,3);
        mXrcv_show.addItemDecoration(new MarginDecoration(mXrcv_show,getResources().getDimensionPixelOffset(R.dimen.layout_padding_small),3));
        mXrcv_show.setLayoutManager(manager);
        mXrcv_show.setPullRefreshEnabled(true);
        mXrcv_show.setLoadingMoreEnabled(true);
        mAdapter = new ShowActivityAdapter(mContext,mDatas,R.layout.empty_view,R.layout.item_project);//item_show_activity
    }

    @Override
    protected void initEvents() {

        mXrcv_show.setAdapter(mAdapter);
        mXrcv_show.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);
    }

//    @Override
//    protected void getDatas(Bundle arguments) {
//        if (arguments != null){
//            cid = arguments.getString("cid");
//            if (cid != null){
//                getList();
//            }
//        }
//    }

    //    @Override
    //    protected void loadDatas() {
    //        getList();
    //    }

    /**
     * 获取数据列表
     */
    private void getList() {
        mDatas.add(new ShowActivityListBean.DataBean.ListBean());
        mDatas.add(new ShowActivityListBean.DataBean.ListBean());
        mDatas.add(new ShowActivityListBean.DataBean.ListBean());
        mDatas.add(new ShowActivityListBean.DataBean.ListBean());
        mDatas.add(new ShowActivityListBean.DataBean.ListBean());
        mDatas.add(new ShowActivityListBean.DataBean.ListBean());

        mAdapter.setDatas(mDatas);
        if (isRefresh){
            mXrcv_show.refreshComplete();
        }else {
            mXrcv_show.loadMoreComplete();
        }
    }


    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        currentPage = 1;
        loadDatas();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (currentPage>=totalPage){
            mXrcv_show.setNoMore(true,true,"没有更多数据了");
        }else {
            currentPage++;
            loadDatas();
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        showMsg(position + "");
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }
}
